package logic

import (
	"context"
	"houses_srv/models"
	"time"

	"houses_srv/houses"
	"houses_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateHouseStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateHouseStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHouseStatusLogic {
	return &UpdateHouseStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateHouseStatusLogic) UpdateHouseStatus(in *houses.UpdateHouseStatusRequest) (*houses.UpdateHouseStatusResponse, error) {
	// todo: add your logic here and delete this line
	var h models.House
	house, err := h.ListHouse()
	if err != nil {
		return nil, err
	}

	t := time.Hour * 24

	for _, v := range house {
		houseTime := v.CreatedAt.Add(30 * t)
		if houseTime.Unix() < time.Now().Unix() {
			v.Status = 3
			err := v.UpdateHouse()
			if err != nil {
				return nil, err
			}
		}
	}

	return &houses.UpdateHouseStatusResponse{}, nil
}
